package com.llsydn.beijingnews.menudetailpager;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.llsydn.beijingnews.R;
import com.llsydn.beijingnews.activity.MainActivity;
import com.llsydn.beijingnews.base.BaseMenuDetailPager;
import com.llsydn.beijingnews.domain.NewsBean;
import com.llsydn.beijingnews.menudetailpager.tabdetailpager.TopicDetailPager;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lilinshen
 * @title 专题详情页面
 * @description 请填写相关描述
 * @date 2018/10/25 16:41
 */
public class TopicMenuDetailPager extends BaseMenuDetailPager {
    @ViewInject(R.id.viewpager)
    private ViewPager viewPager;
    @ViewInject(R.id.tablayout)
    private TabLayout tabLayout;
    @ViewInject(R.id.ib_tab_next)
    private ImageButton ib_tab_next;

    // 页签页面的数据集合
    private final List<NewsBean.DataBean.ChildrenBean> children;
    // 页签页面的集合
    private ArrayList<TopicDetailPager> tabDetailPagers;

    public TopicMenuDetailPager(Context context, NewsBean.DataBean dataBean) {
        super(context);
        this.children = dataBean.getChildren();
    }

    /**
     * 初始化页面
     *
     * @return
     */
    @Override
    public View initView() {
        View view = View.inflate(context, R.layout.topicmenu_detail_pager, null);
        x.view().inject(this, view);

        // 下一个按钮设置点击事件
        ib_tab_next.setOnClickListener((v) -> {
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
        });

        return view;
    }

    /**
     * 初始化数据
     */
    @Override
    public void initData() {
        super.initData();
        // 准备新闻详情页面的数据
        tabDetailPagers = new ArrayList<TopicDetailPager>();
        for (int i = 0; i < children.size(); i++) {
            tabDetailPagers.add(new TopicDetailPager(context, children.get(i)));
        }
        // 设置viewpager的适配器
        viewPager.setAdapter(new MyNewsMenuDetailPagerAdapter());
        // 设置indicator
        // tabPageIndicator.setViewPager(viewPager);
        // 监听页面的变化
        // tabPageIndicator.setOnPageChangeListener(new MyOnPageChangeListener());

        // 设置indicator
        tabLayout.setupWithViewPager(viewPager);
        // 监听页面的变化
        viewPager.addOnPageChangeListener(new MyOnPageChangeListener());
        // 设置滑动或者固定
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        // tabLayout自定义布局
        // for (int i = 0; i < tabLayout.getTabCount(); i++) {
        //     TabLayout.Tab tab = tabLayout.getTabAt(i);
        //     tab.setCustomView(getTabView(i));
        //  }

    }

    /**
     * 监听页面的变化
     */
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            if (i == 0) {
                // slidingmenu可以全屏滑动
                isEnableSlidingMenu(SlidingMenu.TOUCHMODE_FULLSCREEN);
            } else {
                // slidingmenu不可滑动
                isEnableSlidingMenu(SlidingMenu.TOUCHMODE_NONE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }

    /**
     * 自定义的adapter
     */
    class MyNewsMenuDetailPagerAdapter extends PagerAdapter {
        // 设置indicator的title
        @Override
        public CharSequence getPageTitle(int position) {
            return children.get(position).getTitle();
        }

        @Override
        public int getCount() {
            return tabDetailPagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TopicDetailPager tabDetailPager = tabDetailPagers.get(position);
            View view = tabDetailPager.rootView;
            container.addView(view);
            // 初始化数据
            tabDetailPager.initData();
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    /**
     * 根据传入的参数是否让slidingmenu可以滑动
     *
     * @param mode
     */
    private void isEnableSlidingMenu(int mode) {
        MainActivity mainActivity = (MainActivity) context;
        mainActivity.getSlidingMenu().setTouchModeAbove(mode);
    }

    /**
     * tabLayout自定义布局
     *
     * @param position
     * @return
     */
    private View getTabView(int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);
        TextView tv = (TextView) view.findViewById(R.id.textView);
        tv.setText(children.get(position).getTitle());
        ImageView img = (ImageView) view.findViewById(R.id.imageView);
        img.setImageResource(R.drawable.dot_focus);
        return view;
    }

}
